<?php

class Admin_CarteirinhaController extends App_Controller_Action
{

    private $_table     = 'ALUNO';
    private $_pk        = 'ALU_ID';
    private $_order_col = 'ALU_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'ALU_STA';
    private $obj        = '';
    private $form       = '';
    public  $dir        = 'public/upload/aluno/';
    public  $_file_col  = 'ALU_FILE';
    public  $db = '';
    
    public function init()
    {
        $this->obj = new Admin_Model_DbTable_Aluno();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Aluno();
        $this->db = Zend_Db_Table_Abstract::getDefaultAdapter();   
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_carteirinha.js?1=3');
        
        $this->view->db = $this->db;
        
        $where = ' a.'.$this->_pk . ' > 0';
        
        $query = "SELECT SQL_CACHE distinct(a.ALU_ID), a.ALU_NOM, a.ALU_CPF
        FROM ALUNO as a
        INNER JOIN MATRICULA as m ON m.ALU_ID = a.ALU_ID AND m.MAT_STA = 1 
        INNER JOIN CURSO as c ON m.CUR_ID = c.CUR_ID AND c.CUR_STA = 1 
        WHERE ".$where." ORDER BY a.".$this->_order_col." ".$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $result;
        
    }
    
    public function imprimirAction() {
        
        $this->_helper->layout->disableLayout(); 
                
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/js/jquery-1.9.1.min.js');
        
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('tools','library');
        
        $this->view->db = $this->db;
        
        $sessao = new Zend_Session_Namespace('Parametros');
        $PAR_MRCIMP = $sessao->parametros['PAR_MRCIMP'];
        $this->_helper->viewRenderer->setRender('imprimir-'.$PAR_MRCIMP);
        
        $id = $this->_getParam('id', 0);
        $cur_id = $this->_getParam('cur_id', 0);
 
                      
        $where = ' a.'.$this->_pk . ' > 0';

        if($id > '') $where .= ' and a.ALU_ID = '. $id;

        if($cur_id > '') $where .= ' and c.CUR_ID = '. $cur_id;

        $query = "SELECT SQL_CACHE distinct(a.ALU_ID), a.ALU_NOM, a.ALU_CPF, m.MAT_ID, cb.CBS_NOM, c.*, n.NRT_NOM 
        FROM ALUNO as a
        INNER JOIN MATRICULA as m ON m.ALU_ID = a.ALU_ID AND m.MAT_STA = 1 
        INNER JOIN CURSO as c ON m.CUR_ID = c.CUR_ID AND c.CUR_STA = 1 
        INNER JOIN CURSOBASE as cb ON c.CBS_ID = cb.CBS_ID 
        LEFT JOIN NUMEROTURMA as n ON c.CUR_TRM = n.NRT_ID 
        WHERE ".$where." ORDER BY a.".$this->_order_col." ".$this->_order;

        $result = $this->db->fetchAll($query);

        $this->view->texto = $result;

        $this->view->id = $id;

        $this->view->cur_id = $cur_id;

      
    }
    
    public function comboalunosAction(){
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = TRUE;
        $html = '';
        
        $where = ' a.'.$this->_pk . ' > 0';
        
        $cur_id = $this->_getParam('cur_id', 0);
        if((int) $cur_id > 0 ) $where .= " and c.CUR_ID = ".$cur_id;
              
            
        $query = "SELECT SQL_CACHE distinct(a.ALU_ID), a.ALU_NOM, a.ALU_CPF
        FROM ALUNO as a
        INNER JOIN MATRICULA as m ON m.ALU_ID = a.ALU_ID AND m.MAT_STA = 1 
        INNER JOIN CURSO as c ON m.CUR_ID = c.CUR_ID AND c.CUR_STA = 1 
        WHERE ".$where." ORDER BY a.".$this->_order_col." ".$this->_order;

        $result = $this->db->fetchAll($query);


        $html = '<select id="ALU_ID" name="ALU_ID" class="chosen-select" >';
        $html .= '<option value="">Todos Alunos</option>';

        foreach ($result as $r) {
            $html .= '<option '.$select.' value="'.$r['ALU_ID'].'">'.$r['ALU_NOM'].'</option>'; // $r['CUR_NOM'] 
        }

        $html .= '</select><script> $(function(){ $(".chosen-select").chosen(); }); </script>';

        echo json_encode(array('ok' => $ok,'html'=>$html));
    }

}